//


#import "AM_HomeDetailViewController.h"
#import "AM_HomeDetailView.h"
#import "AM_HomeAddViewController.h"

//
@interface AM_HomeDetailViewController ()<HomeDetailViewDelegate>

/** <#Description#>*/
@property (nonatomic, strong) AM_CoreDataView *fmdb;
@property (nonatomic, strong) AM_CoreDataView *fmdbTwo;
/** <#Description#>*/
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#Description#>*/
@property (nonatomic, strong) AM_HomeDetailView *detailView;

@end

@implementation AM_HomeDetailViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    PostNotifition(@"Hidden");
    _fmdb = [[AM_CoreDataView alloc]initWithCoreData:@"HomeData" modelName:fmdt success:^{} fail:^(NSError * _Nullable error) {}];
    [self getfmData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    PostNotifition(@"show");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setnavBatTitle:@"Detail"];
    self.view.backgroundColor = RGB(245, 245, 245);
    
    _fmdbTwo = [[AM_CoreDataView alloc]initWithCoreData:@"DeleteData" modelName:fmdt success:^{} fail:^(NSError * _Nullable error) {}];
    //
    //
    __weak typeof(self) weakSelf = self;
    [self setupLeftAndwqwRightSetting:@"Edit" block:^{
        AM_HomeAddViewController *vc = [[AM_HomeAddViewController alloc]init];
        UINavigationController * navigation = [[UINavigationController alloc]initWithRootViewController:vc];
        vc.model = weakSelf.dataArray.firstObject;
        [weakSelf presentViewController:navigation animated:YES completion:nil];
    }];
    
    [self createUIView];
    
}
//MARK:自定义view
- (void)createUIView {
    //
    _detailView = [[AM_HomeDetailView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT + kRelativeHeight(10), kMainScreenWidth, kMainScreenHeight)];
    _detailView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_detailView];
    _detailView.delegate = self;
    
}
#pragma mark ----------------- 自定义代理 ----------------
- (void)setDeleteData:(AM_HomeDetailView *)view {
    //当前时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSDate *datenow = [NSDate date];
    NSString  *currentTimeString = [formatter stringFromDate:datenow];
    //
    __weak typeof(self) weakSelf = self;
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:@"Do you want to delete ?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * sureAction = [UIAlertAction  actionWithTitle:@"delete"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction){
        //删除数据
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [weakSelf.fmdb am_deleteEntity:nil key:[NSString stringWithFormat:@"id = %@",weakSelf.model.id] success:^{
            //
            [dic setObject:weakSelf.model.name forKey:@"name"];
            [dic setObject:weakSelf.model.account forKey:@"account"];
            [dic setObject:currentTimeString forKey:@"date"];
            [dic setObject:@"1" forKey:@"id"];
            //把删除的数据再存进另一个表里
            [weakSelf.fmdbTwo am_insertNewEntity:dic success:^{} fail:^(NSError * _Nullable error) {}];
            //
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        } fail:^(NSError * _Nullable error) {
            
        }];
        
    }];
    UIAlertAction * cancelAction = [UIAlertAction  actionWithTitle:@"cancel"style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction){}];
    [alertC addAction:sureAction];
    [alertC addAction:cancelAction];
    [self presentViewController:alertC animated:YES completion:nil];
    
}
//
- (void)getfmData {
    _dataArray = [NSMutableArray new];
    
    [_fmdb am_readEntity:nil ascending:NO filterStr:[NSString stringWithFormat:@"id=%@",self.model.id] success:^(NSArray * _Nonnull results) {
        if (!results)return ;
        self.dataArray = [results copy];
        self.detailView.model = self.dataArray.firstObject;
        
    } fail:^(NSError * _Nullable error) {
        
    }];
}

@end
